﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Net.Configuration;

namespace SalePartial.Utility
{
    public class MailSendInfo
    {
        public string MailFromAddr {get;set;}
        public string MailFromDispNm {get;set;}
        public string MailFromPwd {get;set;}
        
        public string MailToAddr { get; set; }
        public string MailToDispNm { get; set; }

        public string MailSmtpHost {get;set;}
        public int MailSmtpPort {get;set;}
        public string MailSubject {get;set;}
        public string MailBody {get;set;}
        public string Other { get; set; }
    }

    public class MailSendUtility
    {

        //private static string KEY_USERNAME="$USERNAME";


        //private static string MAIL_FROM_ADDR = "XXXX@gmail.com";
        //private static string MAIL_FROM_DISP_NM = "ショウ";
        //private static string MAIL_FROM_PWD = "XXXX";
        //private static string MAIL_SMTP_HOST = "smtp.gmail.com";
        //private static int MAIL_SMTP_PORT = 587;

        //private static string MAIL_SUBJECT = "テスト";


        public static bool sendMail(MailSendInfo mailSendInfo)
        {
            MailMessage mm = new MailMessage();
            //送信者
            mm.From = new MailAddress(mailSendInfo.MailFromAddr, mailSendInfo.MailFromDispNm);
            //送信先
            mm.To.Add(new MailAddress(mailSendInfo.MailToAddr, mailSendInfo.MailToDispNm));
            //題名
            mm.Subject = mailSendInfo.MailSubject;
            //本文
            mm.Body = mailSendInfo.MailBody;
            //JISコードに変換する
            mm.BodyEncoding = Encoding.GetEncoding("utf-8");
            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = mailSendInfo.MailSmtpHost;
                smtp.Port = mailSendInfo.MailSmtpPort; //GMail認証
                smtp.Credentials = new NetworkCredential(mailSendInfo.MailFromAddr, mailSendInfo.MailFromPwd); 
                //SSL
                smtp.EnableSsl = true;
                //メッセージを送信する
                smtp.Send(mm);

            }
            catch (Exception e)
            {
            }
            finally
            {
                //後始末
                mm.Dispose();
            }

            return true;
        }

        public static bool SyncSendMail(string strMailToAddr, string strMailSubject, string strMailBody)
        {

            MailSendInfo mailSendInfo = new MailSendInfo();

            SmtpSection smtpSec = GlobalSetting.MailSettings;

            mailSendInfo.MailFromAddr = smtpSec.Network.UserName;//smtpSec.From;
            //mailSendInfo.MailFromDispNm = "";
            mailSendInfo.MailFromPwd = smtpSec.Network.Password;

            mailSendInfo.MailToAddr = strMailToAddr;
            //mailSendInfo.MailToDispNm = strMailToDispNm;

            mailSendInfo.MailSmtpHost = smtpSec.Network.Host;
            mailSendInfo.MailSmtpPort = smtpSec.Network.Port;
            mailSendInfo.MailSubject = strMailSubject;
            mailSendInfo.MailBody = strMailBody;
            //mailSendInfo.Other = strOther;

            SendLoginSuccessMailDelegate loginOkMail = new SendLoginSuccessMailDelegate(LoginOkWorker);
            loginOkMail.BeginInvoke(mailSendInfo, new AsyncCallback(CallBackMethod), null);

            return true;
        }

        private static void CallBackMethod(IAsyncResult result)
        {
            SendLoginSuccessMailDelegate deleg = ((AsyncResult)result).AsyncDelegate as SendLoginSuccessMailDelegate;
            int isResult = deleg.EndInvoke(result);
            // 失敗場合
            if (isResult == -1)
            {
                //this.clsForm.BeginInvoke(new runEndDelegate(runFailed), null);
            }
            else
            {
                //statusLabelMsg.Text = string.Format("{0}件目テストデータ作成完了...", isResult);

            }
            //PROCESS_CUR += 1;
            //this.BeginInvoke(new UpdateProgressBarDelegate(UpdateProgressBar), new object[] { PROCESS_CUR });
        }

        private  delegate int SendLoginSuccessMailDelegate(MailSendInfo mailSendInfo);
        private static int LoginOkWorker(MailSendInfo mailSendInfo)
        {

            ////SynchMsg(statusLabelMsg, string.Format("{0}件目テストデータ原型コピー中...", offset));
            //mailSendInfo.MailFromAddr = MAIL_FROM_ADDR;
            //mailSendInfo.MailFromDispNm = MAIL_FROM_DISP_NM;
            //mailSendInfo.MailFromPwd =MAIL_FROM_PWD;
            //mailSendInfo.MailSmtpHost =MAIL_SMTP_HOST;
            //mailSendInfo.MailSmtpPort = MAIL_SMTP_PORT;
            //mailSendInfo.MailSubject = MAIL_SUBJECT;

            //string strOrgMailContent = readMailContent("MailContent.txt");

            //if (strOrgMailContent == null)
            //{
            //    return -1;
            //}
            //mailSendInfo.MailBody = strOrgMailContent.Replace(KEY_USERNAME, mailSendInfo.MailToDispNm);

            if(!sendMail(mailSendInfo))
            {
                return -1;
            }

            return 0;

        }
        
        private static string readMailContent(string path)
        {
            if (path == null || path == string.Empty || !File.Exists(path))
            {
                return null;
            }

            return File.ReadAllText(path,Encoding.UTF8);
        }
    }
}
